//
//  CouponDetailViewController.m
//  EasyToo
//
//  Created by Jr Lan on 13-9-21.
//
//

#import "CouponDetailViewController.h"
#import "MapViewController.h"
#import <QuartzCore/QuartzCore.h>

@interface CouponDetailViewController ()

@end

@implementation CouponDetailViewController
{
    UIAlertView *_baseAlert;
    int tagid;
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    //!!!!!!
    //[self.navigationItem setTitle:self.couponInfo.place];
    //加载视图
    [self init_view];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//加载视图
- (void)init_view
{
    int width = [DeviceUtil getScreenWidth];
    int heigth = self.view.frame.size.height;
    
    UIScrollView *myscrollview = [[[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, width, heigth)]autorelease];
    myscrollview.directionalLockEnabled = YES; //只能一个方向滑动
    myscrollview.pagingEnabled = NO; //是否翻页
    [myscrollview setBackgroundColor:BackgroundColor];
    myscrollview.showsVerticalScrollIndicator =YES; //垂直方向的滚动指示
    myscrollview.indicatorStyle = UIScrollViewIndicatorStyleWhite;//滚动指示的风格
    myscrollview.showsHorizontalScrollIndicator = NO;//水平方向的滚动指示
    CGSize newSize = CGSizeMake(width, 150+180*[self.couponArray count]+[self.shopInfos count]*60);
    [myscrollview setContentSize:newSize];

    //加载背影
    [self addCouponDetail:myscrollview];
//    UIButton *bgBtn =[UIButton buttonWithType:UIButtonTypeCustom];
//    bgBtn.frame = CGRectMake(10, 20, 300, 140);
//    [bgBtn setBackgroundImage:[UIImage imageNamed:@"flightticket1_03"] forState:UIControlStateNormal];
//    [myscrollview addSubview:bgBtn];

    //加载门店
    [self addStore:myscrollview];
    
    [self.view addSubview:myscrollview];
}

//加载优惠券信息
- (void)addCouponDetail:(UIScrollView*)myscrollview
{
    for (int i = 0; i < [self.couponArray count]; i++) {
        CouponInfo *couponinfo = [self.couponArray objectAtIndex:i];
        
        //图片
        UIImageView *imgView = [[[UIImageView alloc] initWithFrame:CGRectMake(10, 10 + 180*i, 300, 160)]autorelease];
        imgView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        imgView.image = [UIImage imageNamed:@"couponbg"];
        [myscrollview addSubview:imgView];
        
        //详情
        UILabel *desclbl = [[[UILabel alloc]initWithFrame:CGRectMake(40, 38 + 180*i, 240, 23)]autorelease];
        desclbl.text = couponinfo.information;
        desclbl.font = [UIFont systemFontOfSize:14];
        desclbl.backgroundColor = [UIColor clearColor];
        desclbl.adjustsFontSizeToFitWidth = YES;
        [myscrollview addSubview:desclbl];
        
        //地址
        UILabel *addresslbl = [[[UILabel alloc]initWithFrame:CGRectMake(40, 61 + 180*i, 240, 23)]autorelease];
        addresslbl.text = couponinfo.detail;
        addresslbl.font = [UIFont systemFontOfSize:14];
        addresslbl.backgroundColor = [UIColor clearColor];
        addresslbl.adjustsFontSizeToFitWidth = YES;
        [myscrollview addSubview:addresslbl];
        
        //日期
        UILabel *datelbl = [[[UILabel alloc]initWithFrame:CGRectMake(40, 84 + 180*i, 240, 23)]autorelease];
        datelbl.text = couponinfo.endTime;
        datelbl.font = [UIFont systemFontOfSize:14];
        datelbl.backgroundColor = [UIColor clearColor];
        datelbl.adjustsFontSizeToFitWidth = YES;
        [myscrollview addSubview:datelbl];
        
        //添加按钮
        UIButton *nextStep =[UIButton buttonWithType:UIButtonTypeCustom];
        nextStep.tag = [couponinfo.couponId intValue];
        nextStep.frame = CGRectMake(30, 107 + 180*i, 260, 40);
        [nextStep setBackgroundImage:[UIImage imageNamed:@"xyb"] forState:UIControlStateNormal];
        [nextStep setTitle:@"发送到手机" forState:UIControlStateNormal];
        nextStep.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
        nextStep.layer.borderColor = [UIColor grayColor].CGColor;
        nextStep.layer.borderWidth = 1 ;
        nextStep.layer.cornerRadius = 10 ;
        [myscrollview addSubview:nextStep];
        [nextStep addTarget:self action:@selector(confirmMessge:) forControlEvents:UIControlEventTouchUpInside];
    }
}

//加载门店
- (void)addStore:(UIScrollView*)myscrollview
{
    int begin = [self.couponArray count]*180;
    UILabel *titlelbl = [[[UILabel alloc]initWithFrame:CGRectMake(10, begin + 20, 300, 30)]autorelease];
    titlelbl.text = @"    门店信息";
    titlelbl.font = [UIFont systemFontOfSize:16];
    titlelbl.layer.borderColor = [UIColor grayColor].CGColor;
    titlelbl.layer.borderWidth = 1 ;
    [myscrollview addSubview:titlelbl];
    
    for (int i = 0; i < [self.shopInfos count]; i++) {
        UIButton *tmpBtn = [[[UIButton alloc]initWithFrame:CGRectMake(10, begin + 50 + 60*i, 300, 60)]autorelease];
        UILabel *desclbl = [[[UILabel alloc]initWithFrame:CGRectMake(10, begin + 50 + 60*i, 300, 60)]autorelease];
        Shop *shopinfo = [self.shopInfos objectAtIndex:i];
        desclbl.text = [NSString stringWithFormat:@"    %@\n    %@", shopinfo.name, shopinfo.address];
        desclbl.font = [UIFont systemFontOfSize:14];
        desclbl.lineBreakMode = UILineBreakModeWordWrap;
        desclbl.numberOfLines = 0;
        desclbl.adjustsFontSizeToFitWidth = YES;
        desclbl.layer.borderColor = [UIColor grayColor].CGColor;
        desclbl.layer.borderWidth = 1;
        [tmpBtn addTarget:self action:@selector(gotoMapEvent) forControlEvents:UIControlEventTouchUpInside];
        [myscrollview addSubview:tmpBtn];
        [myscrollview addSubview:desclbl];
    }
}

//跳转到地图
- (void)gotoMapEvent
{
    MapViewController *mapController = [[MapViewController alloc] init];
    [self.navigationController pushViewController:mapController animated:YES];
    [mapController release];
}

//输入手机号
- (void)confirmMessge:(id)sender
{
    UIButton *button = (UIButton *)sender;
    tagid = button.tag;
    UIAlertView *prompt = [[UIAlertView alloc] initWithTitle:@"请输入手机号:"
                                                     message:@""
                                                    delegate:nil
                                           cancelButtonTitle:@"取消"
                                           otherButtonTitles:@"确定", nil];
    prompt.alertViewStyle = UIAlertViewStylePlainTextInput;
    prompt.delegate = self;
    [prompt setTransform:CGAffineTransformMakeTranslation(0.0, -100.0)];  //可以调整弹出框在屏幕上的位置
    [prompt show];
    [prompt release];
}

//请求发送优惠券
- (void) alertView:(UIAlertView*)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    //得到输入框
    UITextField *mobileTF=[alertView textFieldAtIndex:0];
    
    //判断手机号码格式
    if ([DeviceUtil isMobileNumber:mobileTF.text] == NO) {
        _baseAlert = [[UIAlertView alloc]  initWithTitle:@"号码出错" message:@"手机号格式出错"  delegate:self cancelButtonTitle:@"返回" otherButtonTitles: nil];
        [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(performDismiss:) userInfo:nil repeats:NO];
        [_baseAlert show];
    }
    else{
        //请求数据
        CouponManager *couponmanager = [CouponManager shareManager];
        couponmanager.delegate = self;
        [couponmanager sendCoupon:[NSString stringWithFormat:@"%d", tagid] telNum:mobileTF.text];
    }
    
}

- (void) performDismiss: (NSTimer *)timer {
    [_baseAlert dismissWithClickedButtonIndex:0 animated:NO];//important
    [_baseAlert release];
    _baseAlert = NULL;
}

#pragma networking
//发送优惠券
- (void)sendCouponFinish:(BOOL)isSuccess
{
    if (isSuccess) {
        _baseAlert = [[UIAlertView alloc]  initWithTitle:@"发送优惠券" message:@"优惠券已发送到您的手机"  delegate:self cancelButtonTitle:@"返回" otherButtonTitles: nil];
    } else {
        _baseAlert = [[UIAlertView alloc]  initWithTitle:@"发送优惠券" message:@"优惠券发送失败，请重新尝试"  delegate:self cancelButtonTitle:@"返回" otherButtonTitles: nil];
    }
    [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(performDismiss:) userInfo:nil repeats:NO];
    [_baseAlert show];
}

@end


